$(function () {
    //获取token
    var token = localStorage.token;
    if(!token) {
        location.href = "../loginAndReg/login.html";
    };
    //获取购物车列表
    $.ajax({
        type: "get",
        url: domain.test + "/getcarlist",
        async:false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("token", token);
        },
        success: function (response) {
            if (response.data.rtnCode === 200) {
                var carList = response.data.data;
                $(".carList").html(template("carList", { carList: carList }));
                $(".emptyCarBox").html(template("emptyCarBox", { length: carList.length }));
                if (carList.length === 0) {
                    window.hasStatus = true;
                }
            }
        }
    });
    //列表左右滑动
    var startX,animateX,endX;
    $(".carList").on("touchstart",".scrollBox",function(e) {
        startX = e.changedTouches[0].clientX;
    });
    $(".carList").on("touchmove",".scrollBox",function(e) {
        animateX = e.changedTouches[0].clientX;
        // $(this).css("left",animateX-startX);
    });
    $(".carList").on("touchend",".scrollBox",function(e) {
        endX = e.changedTouches[0].clientX;
        // $(this).css("left",animateX-startX);
        if(Math.abs(startX-endX)>10) {
            var temp = parseFloat($(".functionBtn").css("width"));
            if(startX-endX>0) {
                var left = parseFloat($(this).css("left"));
                if(left===0) {
                    $(this).animate({
                        "left":-temp
                    },300);
                }
               
            }
            if(startX-endX<0) {
                var left = parseFloat($(this).css("left"));
                if(left===-temp) {
                    $(this).animate({
                        "left":0
                    },300);
                } 
            }
        }
    });
    //猜你喜欢
    var flag = false;
    var pageNum = 1;
    function loadLikeProduct() {
        if (flag) return false;
        $.ajax({
            url: domain.test + "/getlike",
            // url: domain.online + "/api/index/like",
            type: 'get',
            data: 'pageNum=' + pageNum,
            dataType: 'json',
            beforeSend: function () {
                flag = true;
            },
            success: function (response) {
                var likeProductDetail = response.data.data;
                // var likeProductDetail = response.data;
                if (pageNum == 1) {
                    $('.itemBox').html(template('itemBox', { productList: likeProductDetail.plist }));
                } else {
                    $('.itemBox').append(template('itemBox', { productList: likeProductDetail.plist }));
                }
                pullmaster.pullDownSuccess();       //刷新成功
                if (likeProductDetail.plist.length < 10 || likeProductDetail.plist.length === 0) {
                    pullmaster.pullUpDone();        //到底啦
                    $(".bb_pull-up").css({ "display": "none" });
                    $(".end").css({ "display": "block" });
                }
                ++pageNum;
                flag = false;
            },
            error: function () {
                pullmaster.pullDownFailed();        //刷新失败
                pullmaster.pullUpFailed();          //数据加载失败
            }
        })
    }
    // 下拉刷新上滑分页加载
    if(window.hasStatus) {
        $(".likeIcon").css("display","block");
        var pullmaster = new Pull($('.likeProduct'), {
            onPullUp: function () {
                loadLikeProduct();
            },
            onPullDown:function() {
                pageNum = 1;
                loadLikeProduct();
            }
        });
    }
    //查看详情
    $(".itemBox").on("click", ".likeProductItem", function () {
        var pId = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + pId;
    })
})